package com.payment.ktb.Fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.adapter.AgentManageAgentInfoAdapter;
import com.payment.ktb.adapter.CustomerManageAdapter;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.AgentManageAgentInfoEntity;
import com.payment.ktb.model.CustomerManageEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.LogUtil;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/12/4.
 */

public class FragmentAgentManageAgentInfo extends BaseFragment {


    private View view;
    private PullToRefreshListView prlv_fragmentagentmanageagentinfo;
    private int page = 0;
    private List<AgentManageAgentInfoEntity> listDatas = new ArrayList<>();
    private AgentManageAgentInfoAdapter adapter;
    private String orgType = "L31";
    private Spinner sp_fragmentagentmanageagentinfo_agentlevel;
    private TextView tv_fragmentagentmanageagentinfo_agentname;
    private TextView tv_fragmentagentmanageagentinfo_agentcount;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragmentagentmanageagentinfo,null);

        initData();

        prlv_fragmentagentmanageagentinfo.setMode(PullToRefreshBase.Mode.BOTH);
        prlv_fragmentagentmanageagentinfo.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉刷新
                pullDownToRefresh();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pullUptoLoad();


            }
        });
        pullDownToRefresh();
        return view;


    }

    private void initData() {
        prlv_fragmentagentmanageagentinfo = (PullToRefreshListView) view.findViewById(R.id.prlv_fragmentagentmanageagentinfo);
        tv_fragmentagentmanageagentinfo_agentname = (TextView) view.findViewById(R.id.tv_fragmentagentmanageagentinfo_agentname);
        tv_fragmentagentmanageagentinfo_agentcount = (TextView) view.findViewById(R.id.tv_fragmentagentmanageagentinfo_agentcount);
        sp_fragmentagentmanageagentinfo_agentlevel = (Spinner) view.findViewById(R.id.sp_fragmentagentmanageagentinfo_agentlevel);
        sp_fragmentagentmanageagentinfo_agentlevel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] spagentinfo = getResources().getStringArray(R.array.spagentinfo);
                if (i == 0) {
                    orgType = "L31";
                }else if (i == 1){
                    orgType = "L32";
                }
                tv_fragmentagentmanageagentinfo_agentname.setText("个" + spagentinfo[i]);
                pullDownToRefresh();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        // 设置下拉刷新加载文本
        prlv_fragmentagentmanageagentinfo.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
        prlv_fragmentagentmanageagentinfo.getLoadingLayoutProxy(true, false).setReleaseLabel("放开刷新...");
        prlv_fragmentagentmanageagentinfo.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
        // 设置上拉加载文本
        prlv_fragmentagentmanageagentinfo.getLoadingLayoutProxy(false, true).setPullLabel("上拉刷新...");
        prlv_fragmentagentmanageagentinfo.getLoadingLayoutProxy(false, true).setReleaseLabel("放开刷新...");
        prlv_fragmentagentmanageagentinfo.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
    }



    private void pullDownToRefresh() {

        progressDialogUtils.showProgress();
        //构造传输参数
        page = 0;
        Map<String, String> map = new HashMap<String,String>();
        map.put("orgType", orgType);
        map.put("limit", "20");
        map.put("page", String.valueOf(page));
        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgChildInfo, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    progressDialogUtils.dismissProgress();
                    prlv_fragmentagentmanageagentinfo.onRefreshComplete();
                    listDatas.clear();

                    //data
                    listDatas = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<AgentManageAgentInfoEntity>>() {}.getType());
                    if (listDatas == null || listDatas.isEmpty()) {
                        listDatas = new ArrayList<AgentManageAgentInfoEntity>();
                        ToastUtils.showToast("没有数据！");
                    }
                    tv_fragmentagentmanageagentinfo_agentcount.setText(jsonObject.optString("allCount"));
                    //adapter
                    adapter = new AgentManageAgentInfoAdapter(mContext, listDatas);
                    //bind
                    prlv_fragmentagentmanageagentinfo.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }

            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_fragmentagentmanageagentinfo.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }




    private void pullUptoLoad() {

        progressDialogUtils.showProgress();
        //构造传输参数
        page = page + 1;
        Map<String, String> map = new HashMap<String,String>();

        map.put("orgType", orgType);
        map.put("limit", "20");
        map.put("page", String.valueOf(page));
        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgChildInfo, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                try {

                    JSONObject jsonObject = new JSONObject(response);

                    progressDialogUtils.dismissProgress();
                    prlv_fragmentagentmanageagentinfo.onRefreshComplete();
                    //data
                    List<AgentManageAgentInfoEntity> AgentInfos = null;
                    AgentInfos = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<AgentManageAgentInfoEntity>>() {}.getType());

                    if (AgentInfos != null && !AgentInfos.isEmpty()) {
                        //adapter
                        listDatas.addAll(AgentInfos);
                        tv_fragmentagentmanageagentinfo_agentcount.setText(jsonObject.optString("allCount"));
                        adapter.notifyDataSetChanged();
                    }else {
                        ToastUtils.showToast("没有更多数据啦！");
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }

            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_fragmentagentmanageagentinfo.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }







}
